package com.codes.service.impl;

import com.codes.dao.LogisticsMapper;
import com.codes.dao.OrderInfoMapper;
import com.codes.dao.WorkOrderMapper;
import com.codes.model.Logistics;
import com.codes.service.LogisticsSV;
import com.codes.service.MessageSV;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Date 2020/5/29 11:51
 * @Created by peanut
 */
@Service
@Slf4j
public class LogisticsImpl implements LogisticsSV {

    @Autowired
    private LogisticsMapper logisticsMapper;
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private WorkOrderMapper workOrderMapper;
    @Autowired
    private MessageSV messageSV;


    @Override
    public int deleteByPrimaryKey(Integer id) {
        return logisticsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteByWorkOrderId(Integer m) {
        return deleteByWorkOrderId(m);
    }

    @Override
    public int insert(Logistics record) {
        return 0;
    }

    @Override
    public int insertSelective(Logistics record) {
        return 0;
    }

    @Override
    public Logistics selectByPrimaryKey(Integer id) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(Logistics record) {
        return logisticsMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Logistics record) {
        return 0;
    }

    @Override
    public int insertByMap(Map<String, Object> map) {
        //


        return  logisticsMapper.insertByMap(map);
    }

    @Override
    public int insertLogisticeInfo(Map<String, Object> map) {
        int re = 0;
        // workOrderid
        Integer workOrderId = MapUtils.getInteger(map,"workOrderId");
        if(null == workOrderId) return 0;
        re = workOrderMapper.updateByPrimaryKeySelective(map);
        List<Map<String,Object>> logisticsList  = (ArrayList) map.get("logisticsInfo");
        if(null != logisticsList && logisticsList.size()>0){
            // 获取所有物流单
            for (Map m: logisticsList) {
                //  三车.    每车 有 多个商品,先保存物流单,然后保存物流单详情, 然后发消息给司机
                m.put("workOrderId",workOrderId);
                re = logisticsMapper.updateByMap(m);
                log.info("修改物流单:分配司机");
                Integer logisticsId = MapUtils.getInteger(m,"logisticsId");
                log.info("物流单id:"+logisticsId);
                //  物流单保存, 发送消息给司机
                Map oMap =  workOrderMapper.getById(workOrderId);
                oMap.put("logisticsId",logisticsId);
                oMap.put("workOrderId",0);
                Integer receiveUser = MapUtils.getInteger(m,"driverId");
                oMap.put("receiveUser",receiveUser);
                messageSV.insertSelective(oMap);
                log.info("发送消息给司机");
//            // 商品详情列表
//            List<Map<String,Object>> productList  = (ArrayList) m.get("productList");
//            if(productList.size() > 0){
//                // 说明有商品  开始保存详情
//                for ( Map mm: productList) {
//                    // 物流单保存后返回id 保存到详情表
//                    mm.put("logisticsId",logisticsId);
//                    mm.put("workOrderId",null);// 需求工单id 设置为null
//                    re =   orderInfoMapper.insertByMap(mm);
//                }
//            }
            }
        }

        return re;
    }

    @Override
    public List<Map<String, Object>> getLogisticsByOrderId(Map map) {

        return logisticsMapper.getLogisticsByOrderId(map);
    }

    @Override
    public List<Map<String, Object>> getListByDriverId(Map map) {
        return logisticsMapper.getListByDriverId(map);
    }

    @Override
    public Map getInfoByLogisticsId(Map m) {

        List<Map<String,Object>> logOrderInfo = orderInfoMapper.selectByOrderId(m);
        m = logisticsMapper.getInfoByLogisticsId(m);
        if(null != logOrderInfo){

            m.put("logOrderInfo",logOrderInfo);
        }



        return m;
    }

    @Override
    public int updateByMap(Map map) {
        return logisticsMapper.updateByMap(map);
    }

    @Override
    public int getCountByOrderId(Map map) {
        return logisticsMapper.getCountByOrderId(map);
    }

    @Override
    public int getCount(Map map) {
        return logisticsMapper.getCount(map);
    }

    @Override
    public List<Map<String, Object>> getListByRole(Map m) {
        return logisticsMapper.getListByRole(m);
    }
}